// pages/auth/index.js
import {
  request
} from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
import {
  login
} from "../../utils/asyncWx.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  getUserProfile() {
  try{
    wx.getUserProfile({
      desc: "获取你的昵称，头像，地区及性别",
      success: async (res) => {
        console.log(res);
        const {
          encryptedData,
          rawData,
          iv,
          signature
        } = res;
        const {
          code
        } = await login();
        const loginParams = {
          encryptedData,
          rawData,
          iv,
          signature,
          code
        }
        const {
          token
        } = await request({
          url: "/users/wxlogin",
          data: loginParams,
          method: "post"
        });
        // 4 把token存入缓存中 同时跳转回上一个页面
        wx.setStorageSync("token", token);
        wx.navigateBack({
          delta: 1
        });
      },
    })
  }catch (error) {
    console.log(error);
  }
  }
})